package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.LeaveMapper;
import mianyang.zhangsan.com.pojo.Leave;
import mianyang.zhangsan.com.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("leaveService")
public class LeaveServiceImpl implements LeaveService {
    @Autowired
    private LeaveMapper leaveMapper;

    /**
     * 添加请假
     * @param leave
     */
    @Override
    public void addLeave(Leave leave) {
        leaveMapper.insert(leave);
    }

    /**
     * 分页查询请假
     * @param page
     * @param size
     * @param studentId
     * @return
     */
    @Override
    public Page<Leave> queryByPage(int page, int size, int studentId) {
        if (studentId != 0){
            QueryWrapper<Leave> wrapper = new QueryWrapper<>();
            wrapper.eq("student_id", studentId);
            return leaveMapper.selectPage(new Page<>(page, size), wrapper);
        }
        return leaveMapper.selectPage(new Page<>(page, size), null);
    }

    /**
     * 删除请假
     * @param id
     */
    @Override
    public void deleteLeave(int id) {
        QueryWrapper<Leave> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        leaveMapper.delete(wrapper);
    }

    /**
     * 更新请假信息
     * @param id
     * @param studentId
     * @param info
     */
    @Override
    public void updateLeave(int id, int studentId, String info) {
        QueryWrapper<Leave> wrapper = new QueryWrapper<>();
        Leave leave = new Leave();
        wrapper.eq("id", id);
        if (studentId != 0){
            leave.setStudentId(studentId);
        }
        if (!"".equals(info) && info != null){
            leave.setInfo(info);
        }
        leaveMapper.update(leave, wrapper);
    }
}
